package com.bjpowernode.mvc.service.Impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.bjpowernode.common.client.HttpUtils;
import com.bjpowernode.common.client.RedisClient;
import com.bjpowernode.common.consts.Keys;
import com.bjpowernode.mvc.config.SmsConfig;
import com.bjpowernode.mvc.service.inter.SmsService;
import jakarta.annotation.Resource;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
public class SmsServiceImpl implements SmsService {

    //注入.
    @Resource
    private SmsConfig smsConfig;
    @Resource
    private RedisClient redisClient;

    @Override
    public boolean handleRegisterCode(String phone) {
        boolean add = false;
        //短信的内容
        String code = RandomUtil.randomNumbers(5);
        //验证码是%s,有效时间3分钟
        String content = String.format(smsConfig.getTemplate1(), code);
        //调用发短信方法
        boolean isSend = smsCore(phone, content);
        //根据结果判断是否存储到redis

        if (isSend) {
            add = redisClient.addStr(Keys.SMS_REG_CODE + phone, code, 3);
        }

        System.out.println("盈利宝注册短信验证码:" + code);
        return add;
    }


    //判断验证码
    @Override
    public boolean checkSmsCode(String key, String code) {
        //从redis里面对比
        String saveCode = redisClient.getStr(key);
        if (code != null && code.equals(saveCode)) {
            return true;
        }
        return false;
    }

    //登录.
    @Override
    public boolean handleLoginCode(String phone) {
        boolean add=false;
        //生成验证码的值
        String code=RandomUtil.randomNumbers(5);
        //sms处取模板
        String content=String.format(smsConfig.getTemplate2(),code);
        //调用方法,发送
        boolean isSend=smsCore(phone,content);

        //如果成功.
        if (isSend){
            String key=Keys.SMS_LOGIN_CODE+phone;
           add= redisClient.addStr(key,code,3);

        }
        System.out.println("盈利宝登录短信验证码:" + code);
        return add;
    }

    //调用第三方接口的核心方法
    public boolean smsCore(String phone,String content){
        boolean sendOk = false;

        String host = smsConfig.getHost();
        String path = smsConfig.getPath();
        String method = "POST";
        String appcode = smsConfig.getAppcode();
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        //querys.put("content", "【创信】你的验证码是：5873，3分钟内有效！");
        querys.put("content", content);
        querys.put("mobile", phone);
        Map<String, String> bodys = new HashMap<String, String>();

        try {
            //执行请求，调用第三方接口
            //HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);

            //获取response的body
            //if( response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
            //String responseContent = EntityUtils.toString(response.getEntity());

            String responseContent = "{\n" +
                    "  \"ReturnStatus\": \"Success\",\n" +
                    "  \"Message\": \"ok\",\n" +
                    "  \"RemainPoint\": 420842,\n" +
                    "  \"TaskID\": 18424321,\n" +
                    "  \"SuccessCounts\": 1\n" +
                    "}";

            System.out.println("responseContent = " + responseContent);

            //解析json
            JSONObject jsonContent = JSONUtil.parseObj(responseContent);
            if( "Success".equalsIgnoreCase(jsonContent.getStr("ReturnStatus"))){
                //说明短信下发成功的
                sendOk = true;
            }
            //}

        } catch (Exception e) {
            sendOk = false;
            e.printStackTrace();
        }
        return sendOk;
    }

}
